﻿package mortal.game.view.forging.view
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import baseEngine.modifiers.*;
    import com.mui.controls.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.component.windowTip.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.forging.*;
    import mortal.game.view.forging.data.*;
    import mortal.game.view.forging.renderer.*;
    import mortal.game.view.forging.util.*;
    import mortal.game.view.mainUI.Navbar.*;
    import mortal.mvc.core.*;

    public class EquipDecomposePanel extends ForgingPanelBase
    {
        private var _originList:GTileList;
        private var _decomposeList:GTileList;
        private var _decomposeBtn:GButton;
        private var _pickUpBtn:GButton;
        private var _autoSelBtn:GButton;
        private var _decomposeTip:GTextFiled;
        private var _tempArr:Array;
        private var _productArr:Array;
        private var _effectPlayer:EffectPlayer;
        public static var guiding:Boolean = false;

        public function EquipDecomposePanel(param1:Window = null)
        {
            this._tempArr = [];
            this._productArr = [];
            super(param1);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.createChildren();
            return;
        }// end function

        private function onResLoadCompl(param1 = null) : void
        {
            if (isDisposed)
            {
                return;
            }
            this.createChildren();
            return;
        }// end function

        override protected function createChildren() : void
        {
            super.createChildren();
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.EquipDecompose, 84, 107, this));
            this.pushUIToDisposeVec(UIFactory.bg(36 - 7, 0, 422 + 29, 100 + 17, this, ImagesConst.DecomposeGridBg));
            this.pushUIToDisposeVec(UIFactory.bg(36 - 7, 320, 422 + 14, 56 + 15, this, ImagesConst.DecomposeGridBg));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.EquipDecomposeEffectBg, 52 - 22, 210 - 8, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.Forging_Arrow_Down2, 133 - 22, 109 - 46, this));
            this._decomposeBtn = UIFactory.gButton(Language.getString(41106), 203 - 43 + 47, 398 + 3, 82, 27, this, "BrownBtn");
            this._decomposeBtn.name = "Equip_decomposeBtn";
            this._decomposeBtn.toolTipData = Language.getString(41107);
            this._decomposeBtn.configEventListener(MouseEvent.CLICK, this.onBtnClickHandler);
            this._pickUpBtn = UIFactory.gButton(Language.getString(41108), 203 - 43 + 47, 398 + 3, 82, 27, this, "BrownBtn");
            this._pickUpBtn.configEventListener(MouseEvent.CLICK, this.onBtnClickHandler);
            this._pickUpBtn.name = "Eqip_pickUpBtn";
            this._pickUpBtn.toolTipData = Language.getString(41109);
            this._autoSelBtn = UIFactory.gButton(Language.getString(41110), 203 - 43 + 47, 398 + 3, 82, 27, this, "BrownBtn");
            this._autoSelBtn.name = "forging_autoSelectBtn";
            this._autoSelBtn.configEventListener(MouseEvent.CLICK, this.onBtnClickHandler);
            this._autoSelBtn.toolTipData = Language.getString(41111);
            this._decomposeTip = UIFactory.gTextField("", 15, 409, 176, 20, this, GlobalStyle.textFormatLv);
            this._decomposeTip.htmlText = Language.getString(41112);
            this._originList = UIFactory.tileList(51, 18, 430, 94, this);
            this._originList.columnWidth = 51;
            this._originList.rowHeight = 47;
            this._originList.horizontalGap = 0;
            this._originList.verticalGap = 0;
            this._originList.setStyle("skin", new Bitmap());
            this._originList.setStyle("cellRenderer", EquipDecomposeListCellRenderer);
            this._originList.configEventListener(MouseEvent.CLICK, this.onListClickHandler);
            this._decomposeList = UIFactory.tileList(49, 339, 427, 48, this);
            this._decomposeList.columnWidth = 51;
            this._decomposeList.rowHeight = 47;
            this._decomposeList.horizontalGap = 0;
            this._decomposeList.verticalGap = 0;
            this._decomposeList.setStyle("skin", new Bitmap());
            this._decomposeList.setStyle("cellRenderer", EquipDecomposeListCellRenderer);
            this._decomposeList.configEventListener(MouseEvent.CLICK, this.onResultListClickHandler);
            this.initItemList();
            this.initBtnState();
            this.addListeners();
            this.playDecomposeEffect();
            return;
        }// end function

        private function addListeners() : void
        {
            Dispatcher.addEventListener(EventName.ForgingAddToDecomposePack, this.onAddDecomposeItemHandler);
            NetDispatcher.addCmdListener(ServerCommand.ForgingDecomposeSucc, this.onDecomposeSuccHandler);
            NetDispatcher.addCmdListener(ServerCommand.ForgingDecomposePickSucc, this.onPickUpSuccHandler);
            NetDispatcher.addCmdListener(ServerCommand.ForgingDecomposeBagUpdate, this.onDecomposeBagUpdate);
            Dispatcher.addEventListener(EventName.ForgingUpdateBtnState, this.onUpdateAutoSelBtnState);
            return;
        }// end function

        private function removeListeners() : void
        {
            Dispatcher.removeEventListener(EventName.ForgingAddToDecomposePack, this.onAddDecomposeItemHandler);
            NetDispatcher.removeCmdListener(ServerCommand.ForgingDecomposeSucc, this.onDecomposeSuccHandler);
            NetDispatcher.removeCmdListener(ServerCommand.ForgingDecomposePickSucc, this.onPickUpSuccHandler);
            NetDispatcher.removeCmdListener(ServerCommand.ForgingDecomposeBagUpdate, this.onDecomposeBagUpdate);
            Dispatcher.removeEventListener(EventName.ForgingUpdateBtnState, this.onUpdateAutoSelBtnState);
            return;
        }// end function

        private function initItemList() : void
        {
            var _loc_1:* = new DataProvider();
            var _loc_2:int = 0;
            while (_loc_2 < 16)
            {
                
                _loc_1.addItem({});
                _loc_2++;
            }
            this._originList.dataProvider = _loc_1;
            this._originList.drawNow();
            this.copyDecomposeResultData();
            this.updateDecomposePack();
            return;
        }// end function

        private function initBtnState() : void
        {
            this.updateBtnState();
            return;
        }// end function

        private function updateDecomposeList() : void
        {
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.forging.currDecomposeItems;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                _loc_1.addItem(_loc_2[_loc_3]);
                _loc_3++;
            }
            if (_loc_2.length < ForgingConst.MaxDecomposeItemNum)
            {
                _loc_3 = _loc_2.length;
                while (_loc_3 < ForgingConst.MaxDecomposeItemNum)
                {
                    
                    _loc_1.addItem({});
                    _loc_3++;
                }
            }
            this._originList.dataProvider = _loc_1;
            this._originList.drawNow();
            this.updateBtnState();
            this.updatePreviewProducts();
            this.updateDecomposePack();
            return;
        }// end function

        private function updateDecomposePack() : void
        {
            var _loc_3:* = undefined;
            var _loc_4:Array = null;
            var _loc_5:DataProvider = null;
            var _loc_6:DecomposePreviewItemData = null;
            var _loc_7:int = 0;
            var _loc_1:* = Cache.instance.forging.previewProducts;
            var _loc_2:Array = [];
            for (_loc_3 in _loc_1)
            {
                
                if (_loc_1[_loc_3])
                {
                    _loc_6 = new DecomposePreviewItemData();
                    _loc_6.itemCode = int(_loc_3);
                    _loc_6.itemAmount = _loc_1[_loc_3] as int;
                    _loc_2.push(_loc_6);
                }
            }
            _loc_4 = this._tempArr.concat(_loc_2);
            _loc_5 = new DataProvider();
            if (_loc_4.length < 8)
            {
                _loc_7 = _loc_4.length;
                while (_loc_7 < 8)
                {
                    
                    _loc_4.push({});
                    _loc_7++;
                }
            }
            else if (_loc_4.length > 8)
            {
                _loc_4.splice(0, _loc_4.length - 8);
            }
            _loc_5.addItems(_loc_4);
            this._decomposeList.dataProvider = _loc_5;
            this._decomposeList.drawNow();
            this.updateBtnState();
            return;
        }// end function

        private function updatePreviewProducts() : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:String = null;
            var _loc_6:ItemData = null;
            var _loc_7:Array = null;
            var _loc_8:int = 0;
            var _loc_9:String = null;
            var _loc_10:int = 0;
            var _loc_11:TEquipStrengthen = null;
            var _loc_12:Array = null;
            var _loc_1:* = Cache.instance.forging.currDecomposeItems;
            var _loc_2:* = Cache.instance.forging.clearPreviewProducts();
            for each (_loc_6 in _loc_1)
            {
                
                if (ItemsUtil.isEquip(_loc_6))
                {
                    _loc_5 = _loc_6.itemInfo.decomposeItems;
                    if (_loc_5)
                    {
                        _loc_7 = _loc_5.split("&");
                        if (EquipmentUtil.isStrengthened(_loc_6))
                        {
                            _loc_8 = 0;
                            while (_loc_8 < _loc_7.length)
                            {
                                
                                _loc_9 = _loc_7[_loc_8];
                                _loc_3 = int(_loc_9.split("#")[0]);
                                _loc_4 = int(_loc_9.split("#")[1]);
                                if (StrengUtil.isStrengStone(_loc_3))
                                {
                                    _loc_10 = 1;
                                    while (_loc_10 <= _loc_6.extInfo.strengthen)
                                    {
                                        
                                        _loc_11 = EquipStrengthenConfig.instance.getInfoByLevel(_loc_10);
                                        _loc_4 = _loc_4 + (_loc_11.mustFailedAmout + 1) * _loc_11.consumeItemAmount;
                                        _loc_10++;
                                    }
                                }
                                this.savePreviewData(_loc_6, _loc_3, _loc_4);
                                _loc_8++;
                            }
                        }
                        else
                        {
                            _loc_8 = 0;
                            while (_loc_8 < _loc_7.length)
                            {
                                
                                _loc_9 = _loc_7[_loc_8];
                                _loc_3 = int(_loc_9.split("#")[0]);
                                _loc_4 = int(_loc_9.split("#")[1]);
                                this.savePreviewData(_loc_6, _loc_3, _loc_4);
                                _loc_8++;
                            }
                        }
                    }
                    if (_loc_6.extInfo && _loc_6.extInfo.qual > 0)
                    {
                        _loc_12 = StarAdvanceUtil.getStarAdvanceConsumeInfo(_loc_6);
                        this.savePreviewData(_loc_6, _loc_12[0], _loc_12[1]);
                    }
                    continue;
                }
                if (ItemsUtil.isJewel(_loc_6) || ItemsUtil.isOtherEquip(_loc_6) || ItemsUtil.isHalidomEquip(_loc_6) || ItemsUtil.isOtherSkillBook(_loc_6) || ItemsUtil.isPetSkillBook(_loc_6) || ItemsUtil.isGoldEquipScroll(_loc_6) || ItemsUtil.isRune(_loc_6.itemInfo))
                {
                    _loc_5 = _loc_6.itemInfo.decomposeItems;
                    if (_loc_5)
                    {
                        _loc_3 = int(_loc_5.split("#")[0]);
                        _loc_4 = int(_loc_5.split("#")[1]) * _loc_6.itemAmount;
                    }
                    this.savePreviewData(_loc_6, _loc_3, _loc_4);
                }
            }
            return;
        }// end function

        private function savePreviewData(param1:ItemData, param2:int, param3:int) : void
        {
            var _loc_6:int = 0;
            var _loc_4:* = Cache.instance.forging.previewProducts;
            var _loc_5:* = new ItemData(param2);
            if (!ItemsUtil.isBind(param1))
            {
                if (!_loc_4[param2])
                {
                    _loc_4[param2] = 0;
                }
                _loc_4[param2] = _loc_4[param2] + param3;
            }
            else
            {
                _loc_6 = ItemsUtil.isBind(_loc_5) ? (_loc_5.itemInfo.code) : (_loc_5.itemInfo.codeUnbind);
                if (!_loc_4[_loc_6])
                {
                    _loc_4[_loc_6] = 0;
                }
                _loc_4[_loc_6] = _loc_4[_loc_6] + param3;
            }
            return;
        }// end function

        private function onAddDecomposeItemHandler(event:DataEvent) : void
        {
            this.updateDecomposeList();
            return;
        }// end function

        private function onBtnClickHandler(event:MouseEvent) : void
        {
            var _loc_2:Array = null;
            var _loc_3:Array = null;
            if (event.target == this._decomposeBtn)
            {
                _loc_2 = this.transferData(Cache.instance.forging.currDecomposeItems);
                if (_loc_2.length == 0)
                {
                    MsgManager.showRollTipsMsg(Language.getString(41113));
                    return;
                }
                if (guiding)
                {
                    guiding = false;
                }
                GameProxy.equip.itemDecompose(_loc_2);
            }
            if (event.target == this._pickUpBtn)
            {
                this.updateProduct();
                _loc_3 = this.transferData(Cache.instance.pack.decomposePackCache.getDecomposeResultItems());
                if (_loc_3.length == 0)
                {
                    MsgManager.showRollTipsMsg(Language.getString(41114));
                    return;
                }
                GameProxy.equip.decomposeBagMoveToBag(_loc_3, ForgingConst.PickType_OneKey);
            }
            if (event.target == this._autoSelBtn)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingOnAutoSel));
            }
            return;
        }// end function

        private function transferData(param1:Array) : Array
        {
            var _loc_3:ItemData = null;
            var _loc_4:SBagItem = null;
            var _loc_2:Array = [];
            for each (_loc_3 in param1)
            {
                
                _loc_4 = new SBagItem();
                _loc_4.uid = _loc_3.uid;
                _loc_4.amount = _loc_3.itemAmount;
                _loc_2.push(_loc_4);
            }
            return _loc_2;
        }// end function

        private function onDecomposeSuccHandler(param1:Object) : void
        {
            Cache.instance.forging.clearDecomposeItems();
            this.updateDecomposeList();
            return;
        }// end function

        private function onDecomposeBagUpdate(param1:Object) : void
        {
            this.copyDecomposeResultData();
            this.updateDecomposePack();
            return;
        }// end function

        private function copyDecomposeResultData() : void
        {
            var _loc_2:ItemData = null;
            var _loc_1:* = Cache.instance.pack.decomposePackCache.getDecomposeResultItems();
            this._tempArr.length = 0;
            for each (_loc_2 in _loc_1)
            {
                
                this._tempArr.push(_loc_2);
            }
            return;
        }// end function

        private function onPickUpSuccHandler(param1:int) : void
        {
            if (param1 == ForgingConst.PickType_OneKey)
            {
                this.itemFlyToBag();
            }
            return;
        }// end function

        private function updateProduct() : void
        {
            this._productArr = Cache.instance.pack.decomposePackCache.getDecomposeResultItems();
            return;
        }// end function

        private function itemFlyToBag(param1:Bitmap = null) : void
        {
            var _loc_2:ItemData = null;
            var _loc_3:Point = null;
            var _loc_4:Point = null;
            var _loc_5:Array = null;
            for each (_loc_2 in this._productArr)
            {
                
                if (_loc_2)
                {
                    _loc_3 = (GameController.forging.view as ForgingModule).localToGlobal(new Point(228, 394));
                    _loc_4 = (GameController.navbar.view as NavbarModule).getNavbarIconGlobalPoint(ModuleType.Pack);
                    _loc_5 = [_loc_2, _loc_3, _loc_4, 0.5, 0.5];
                    Dispatcher.dispatchEvent(new DataEvent(EventName.FlyItemToAnyPoint, _loc_5));
                }
            }
            return;
        }// end function

        private function onListClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = this._originList.selectedItem as ItemData;
            if (_loc_2)
            {
                Cache.instance.forging.removeDecomposeItem(_loc_2);
                this.updateDecomposeList();
                Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingCancelAddDecomposeItem, _loc_2));
            }
            return;
        }// end function

        private function onLinkClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = new WindowTipData();
            _loc_2.type = WindowTipType.DECOMPOSE_EXPLAIN;
            _loc_2.tip = Language.getString(40106);
            _loc_2.title = Language.getString(41115);
            _loc_2.tipW = 500;
            _loc_2.tipH = 500;
            WindowTipManager.showTip(_loc_2);
            return;
        }// end function

        private function onOverHandler(event:MouseEvent) : void
        {
            CursorManager.showCursor(CursorManager.BUTTON);
            return;
        }// end function

        private function onOutHandler(event:MouseEvent) : void
        {
            CursorManager.hideFromButton();
            return;
        }// end function

        private function onResultListClickHandler(event:MouseEvent) : void
        {
            var _loc_3:Array = null;
            var _loc_4:Point = null;
            var _loc_5:Point = null;
            var _loc_6:Array = null;
            var _loc_2:* = this._decomposeList.selectedItem as ItemData;
            if (_loc_2)
            {
                if (Cache.instance.pack.backPackCache.isPackFull(_loc_2.itemCode))
                {
                    return MsgManager.showRollTipsMsg(Language.getString(41116), MsgRollTipsType.msgRollTips3);
                }
                _loc_3 = this.transferData([_loc_2]);
                GameProxy.equip.decomposeBagMoveToBag(_loc_3, ForgingConst.PickType_Single);
                _loc_4 = (GameController.forging.view as ForgingModule).localToGlobal(new Point(228, 394));
                _loc_5 = (GameController.navbar.view as NavbarModule).getNavbarIconGlobalPoint(ModuleType.Pack);
                _loc_6 = [_loc_2, _loc_4, _loc_5, 0.5, 0.5];
                Dispatcher.dispatchEvent(new DataEvent(EventName.FlyItemToAnyPoint, _loc_6));
                this.removeFromTempArr(_loc_2);
                this.updateDecomposePack();
            }
            return;
        }// end function

        private function removeFromTempArr(param1:ItemData) : void
        {
            var _loc_2:int = 0;
            if (this._tempArr && this._tempArr.indexOf(param1) != -1)
            {
                _loc_2 = this._tempArr.indexOf(param1);
                this._tempArr.splice(_loc_2, 1);
            }
            return;
        }// end function

        private function playDecomposeEffect() : void
        {
            var _loc_1:* = (_window as ForgingModule).rect3d;
            if (_loc_1)
            {
                _loc_1.disposeObj3d(this._effectPlayer);
                this._effectPlayer = EffectPlayerPool.instance.getEffectPlayer("fbtz-diyushou-huohaiBUFF", null, _loc_1.renderList, false);
                this._effectPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
                _loc_1.addObject3d(this._effectPlayer, 180 + 74, 215 - 34 + 50);
            }
            return;
        }// end function

        private function remove3D() : void
        {
            var _loc_1:* = (_window as ForgingModule).rect3d;
            if (this._effectPlayer)
            {
                _loc_1.disposeObj3d(this._effectPlayer);
                this._effectPlayer = null;
            }
            return;
        }// end function

        private function updateBtnState() : void
        {
            var _loc_1:* = Cache.instance.forging.currDecomposeItems;
            var _loc_2:* = Cache.instance.pack.decomposePackCache.getDecomposeResultItems();
            if (_loc_1.length == 0 && _loc_2.length == 0)
            {
                this._decomposeBtn.visible = false;
                this._pickUpBtn.visible = false;
                this._autoSelBtn.visible = true;
            }
            else if (_loc_1.length != 0)
            {
                this._decomposeBtn.visible = true;
                this._pickUpBtn.visible = false;
                this._autoSelBtn.visible = false;
            }
            else if (_loc_2.length != 0)
            {
                this._decomposeBtn.visible = false;
                this._pickUpBtn.visible = true;
                this._autoSelBtn.visible = false;
            }
            return;
        }// end function

        private function onUpdateAutoSelBtnState(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            if (_loc_2 == 2)
            {
                this.isAutoSelUsable = false;
            }
            else
            {
                this.isAutoSelUsable = true;
            }
            return;
        }// end function

        private function set isAutoSelUsable(param1:Boolean) : void
        {
            if (this._autoSelBtn == null)
            {
                return;
            }
            if (param1)
            {
                this._autoSelBtn.filters = [];
                this._autoSelBtn.mouseEnabled = true;
            }
            else
            {
                this._autoSelBtn.filters = [FilterConst.colorFilter2];
                this._autoSelBtn.mouseEnabled = false;
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (_isCreateChildren)
            {
                _isCreateChildren = false;
                this._decomposeBtn.name = "";
                this._pickUpBtn.name = "";
                this._decomposeBtn.dispose(param1);
                this._originList.dispose(param1);
                this._decomposeList.dispose(param1);
                this._pickUpBtn.dispose(param1);
                this._decomposeTip.dispose();
                this._autoSelBtn.dispose();
                this._decomposeBtn = null;
                this._originList = null;
                this._decomposeList = null;
                this._pickUpBtn = null;
                this._decomposeTip = null;
                this._autoSelBtn = null;
                this._tempArr.length = 0;
                this._productArr.length = 0;
                this.removeListeners();
            }
            Cache.instance.forging.clearDecomposeItems();
            Cache.instance.forging.clearPreviewProducts();
            this.remove3D();
            WindowTipManager.hideTip(WindowTipType.DECOMPOSE_EXPLAIN);
            return;
        }// end function

    }
}
